/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lestr.genver.data.serialization;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pibonnin
 */
class DefaultBeanSerializer implements IBeanSerializer {


    private Class targetType;


    private ArrayList<String> fieldsNames;


    public DefaultBeanSerializer(Class targetType) {

        this.targetType = targetType;
        fieldsNames = null;

        if (!Iterable.class.isAssignableFrom(targetType)) {

            fieldsNames = new ArrayList<String>();

            for (Field fieldInfo : targetType.getFields())
                if (!Modifier.isStatic(fieldInfo.getModifiers())
                    && Modifier.isPublic(fieldInfo.getModifiers()))
                    fieldsNames.add(fieldInfo.getName());

            Collections.sort(fieldsNames);

        }

    }// END Constructor


    @Override
    public Class getTargetType() {

        return targetType;

    }// END Method getTargetType


    @Override
    public Class<?> getFieldType(String fieldName) {

        try {
            return targetType.getDeclaredField(fieldName).getType();
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }

        throw new RuntimeException();

    }// END Method getFieldType


    @Override
    public Object getFieldValue(String fieldName,
                                Object instance) {

        try {
            return targetType.getDeclaredField(fieldName).get(instance);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }

        throw new RuntimeException();

    }// END Method getFieldType


    @Override
    public void setFieldValue(String fieldName,
                              Object instance,
                              Object value) {

        try {
            targetType.getDeclaredField(fieldName).set(instance, value);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DefaultBeanSerializer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }// END Method getFieldType


    @Override
    public Iterable<String> getFieldsNames() {

        return fieldsNames;

    }// END Method getFieldsNames


    @Override
    public boolean isArrayField(String fieldName) {

        throw new UnsupportedOperationException("Not supported yet.");

    }// END Method isArrayField


    @Override
    public Object[] getArrayFieldValues(String fieldName,
                                        Object target) {

        throw new UnsupportedOperationException("Not supported yet.");

    }// END Method getArrayFieldValues


    @Override
    public void setArrayFieldValue(String fieldName,
                                   Object target,
                                   Object[] values) {

        throw new UnsupportedOperationException("Not supported yet.");

    }// END Method setArrayFieldValue


}// END Class DefaultSerializer


